﻿using BLL.Business;
using Entity.Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Services;
using System;
using System.Dynamic;

namespace BLL.ShanXiZH
{
    public class ProcessBuiness
    {
        /// <summary>
        /// 按照交易码，确定请求包类型，并调用不同的业务处理函数。
        /// </summary>
        /// <param name="body"></param>
        public static string DistributeBusiness(dynamic reqData, TradeCode type)
        {
            var result = new ResultModel();
            try
            {
                var ser = IAgentPayment.GetAgentPaymentService();
                switch (type)
                {
                    case TradeCode.缴费查询:
                        {
                            var QueryReqData = new { userId = reqData.USERCODE.ToString().Trim() };
                            result = ser.QueryUserInfo(QueryReqData);
                        }
                        break;
                    case TradeCode.缴费业务:
                        {
                            var payReqData = new
                            {
                                userId = reqData.USERCODE.ToString().Trim(),
                                payMoney = decimal.Parse(reqData.AMOUNT.ToString().Trim()),
                                bankWaterNum = reqData.TRANNUM.ToString().Trim()
                            };
                            result = ser.Pay(payReqData);
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                result = new ResultModel(ResultType.Error, ex.Message);
            }

            dynamic reData = new ExpandoObject();
            reData.RETCODE = result.Code == ResultType.Success ? 1 : 0;
            reData.RETMESSAGE = result.Message;
            if (result.Code == ResultType.Success && type == TradeCode.缴费查询)
            {
                var userInfo = result.Data; 
                reData.USERCODE = reqData.USERCODE.ToString().Trim();
                reData.NAME = userInfo["userName"].ToString();
                reData.ADDRESS = userInfo["userAddress"].ToString();

                // 费用： 如果用户有欠费和滞纳金，则显示应交金额（滞纳金+欠费金额） 反之显示用户余额
                reData.FEE = Convert.ToDecimal(userInfo["znj"]) + Convert.ToDecimal(userInfo["userOwe"]) > 0 ? -(Convert.ToDecimal(userInfo["znj"]) + Convert.ToDecimal(userInfo["userOwe"])) : Convert.ToDecimal(userInfo["userBalance"]);
            }
            return JsonConvert.SerializeObject(reData);
        }
    }
}
